package org.springblade.modules.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.common.cache.DictCache;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.admin.dto.SimpleElevatorDetailDTO;
import org.springblade.modules.admin.entity.Building;
import org.springblade.modules.admin.entity.ElevatorDevice;
import org.springblade.modules.admin.enums.TlwAlarmNoticeEnum;
import org.springblade.modules.admin.mapper.ElevatorDeviceMapper;
import org.springblade.modules.admin.service.ElevatorDeviceService;
import org.springblade.modules.admin.vo.BuildingDeviceEleVO;
import org.springblade.modules.admin.vo.BuildingSelectListVO;
import org.springblade.modules.admin.vo.BuildingVO;
import org.springblade.modules.admin.vo.SimpleElevatorInfoVO;
import org.springblade.modules.system.vo.SimpleUserVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static org.springblade.modules.admin.service.BuildingService.PROJECT_TYPE_CODE;

@Service
public class ElevatorDeviceServiceImpl extends BaseServiceImpl<ElevatorDeviceMapper, ElevatorDevice> implements ElevatorDeviceService{

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean saveOrUpdateElevatorDevice(ElevatorDevice elevatorDevice) {
		if(ObjectUtil.isEmpty(elevatorDevice.getDeviceId())){
			throw new ServiceException("设备ID不可为空");
		}
		if(ObjectUtil.isEmpty(elevatorDevice.getElevatorId())){
			throw new ServiceException("电梯不可为空");
		}
		super.remove(Wrappers.<ElevatorDevice>lambdaQuery().eq(ElevatorDevice::getElevatorId,elevatorDevice.getElevatorId()).or().eq(ElevatorDevice::getDeviceId,elevatorDevice.getDeviceId()));
		return super.save(elevatorDevice);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean saveOrUpdateElevatorDeviceList(List<ElevatorDevice> elevatorDevices) {
		if(ObjectUtil.isEmpty(elevatorDevices)){
			throw new ServiceException("参数不可为空");
		}
		List<Long> elevatorIds = elevatorDevices.stream().map(ElevatorDevice::getElevatorId).collect(Collectors.toList());
		List<String> deviceIds = elevatorDevices.stream().map(ElevatorDevice::getDeviceId).collect(Collectors.toList());
		super.remove(Wrappers.<ElevatorDevice>lambdaQuery().in(ElevatorDevice::getElevatorId,elevatorIds).or().in(ElevatorDevice::getDeviceId,deviceIds));
		return super.saveBatch(elevatorDevices);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean removeElevatorDevice(List<Long> elevatorIds) {
		if(CollUtil.isEmpty(elevatorIds)){
			throw new ServiceException("电梯ID不可为空");
		}
		return super.remove(Wrappers.<ElevatorDevice>lambdaQuery().in(ElevatorDevice::getElevatorId,elevatorIds));
	}

	@Override
	public IPage<BuildingVO> buildingPage(IPage<BuildingVO> page, Building building) {
		IPage<BuildingVO> resultPage = baseMapper.buildingPage(page, building);
		List<BuildingVO> list = resultPage.getRecords();
		for (BuildingVO buildingVO : list) {
			buildingVO.setTypeName(DictCache.getValue(PROJECT_TYPE_CODE, buildingVO.getType()));
		}
		return resultPage;
	}

    @Override
    public List<String> deviceList() {
		return baseMapper.getDeviceList();
    }

	@Override
	public List<SimpleUserVO> getSimpleUserByDeviceId(String deviceId) {
		return baseMapper.getSimpleUserByDeviceId(deviceId);
	}

	@Override
	public SimpleElevatorDetailDTO getSimpleElevator(Long elevatorId, Integer p) {
		if(p.equals(TlwAlarmNoticeEnum.PEOPLE_TYPE.ASSESSOR.getId())){
			return baseMapper.getSimpleElevatorById(elevatorId);
		}
		return baseMapper.getSimpleElevator(elevatorId,p);
	}

	@Override
	public List<SimpleUserVO> getSimpleUserByUserId(List<Long> estateUserIdList) {
		return baseMapper.getSimpleUserByUserId(estateUserIdList);
	}

	@Override
	public List<SimpleUserVO> getSimpleUserByPhone(List<String> assessorPhones) {
		return baseMapper.getSimpleUserByPhone(assessorPhones);
	}

	@Override
	public List<BuildingSelectListVO> buildingSelectDevices() {
		return baseMapper.buildingSelectDevices();

	}

	@Override
	public List<BuildingDeviceEleVO> buildingDeviceEleList(Long buildingId) {
		List<BuildingDeviceEleVO> list = baseMapper.buildingDeviceEleList(buildingId);
		for (int i = 0; i < list.size(); i++) {
			list.get(i).setTypeName(DictCache.getValue(PROJECT_TYPE_CODE, list.get(i).getType()));
		}
		return list;
	}

	@Override
	public List<Map<String, Object>> getSimpleUserByDeviceIds(List<String> deviceIds) {
		return baseMapper.getSimpleUserByDeviceIds(deviceIds);
	}

	@Override
	public List<Map<String, Object>> getSimpleUserByElevatorIds(List<Long> elevatorIds) {
		return baseMapper.getSimpleUserByElevatorIds(elevatorIds);
	}

	@Override
	public SimpleElevatorInfoVO getSimpleElevatorInfo(String deviceId) {
		return baseMapper.getSimpleElevatorInfo(deviceId);
	}

	@Override
	public SimpleElevatorInfoVO getSimpleElevatorInfoByElevatorId(Long elevatorId) {
		return baseMapper.getSimpleElevatorInfoByElevatorId(elevatorId);
	}
}
